# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=mdadm
pkgver=4.2
pkgrel=4
pkgdesc="a tool for managing Linux Software RAID arrays"
url="https://raid.wiki.kernel.org/index.php/A_guide_to_mdadm"
arch="all"
license="GPL-2.0-only"
depends="lsblk"
makedepends="groff linux-headers"
checkdepends="bash eudev e2fsprogs"
subpackages="$pkgname-doc $pkgname-udev::noarch $pkgname-misc::noarch $pkgname-openrc"
source="https://www.kernel.org/pub/linux/utils/raid/mdadm/mdadm-$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname-raid.initd
	lfs64.patch
	no-werror.patch
	mdadm-include-sysmacros.patch
	fix-monitor-tv_sec.patch
	"
options="!check"

build() {
	make CXFLAGS="$CFLAGS -DNO_LIBUDEV"
	make raid6check
}

# This doesn't build in Alpine CI. But it should work on bare-metal as root.
check() {
	export PATH="$PWD:$PATH" # Test needs built binaries
	make test
	./test
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm755 raid6check "$pkgdir"/sbin/raid6check
	install -Dm644 raid6check.8 "$pkgdir"/usr/share/man/man8/raid6check.8

	install -Dm644 $pkgname.conf-example "$pkgdir"/etc/$pkgname.conf
	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname-raid.initd "$pkgdir"/etc/init.d/$pkgname-raid
}

udev() {
	pkgdesc="$pkgdesc (udev rules)"
	install_if="udev $pkgname=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"
	mv "$pkgdir"/lib "$subpkgdir"/
}

misc() {
	pkgdesc="$pkgdesc (misc scripts)"
	depends="$pkgname bash"

	cd "$builddir"

	install -Dm755 misc/mdcheck "$subpkgdir"/usr/sbin/mdcheck
	install -Dm755 misc/syslog-events "$subpkgdir"/usr/sbin/handle-mdadm-events
}

sha512sums="
112a897c6943d7343f44ffe32a8d7ef11e1f1624f51385c0f1d27458d661202bb378678a3ddc78ed2e24533be234441ea15cf33769345d5709b72b72ad9ec540  mdadm-4.2.tar.gz
ca5f4e3ff5b284102b44e818d11622e1889066e3d18edce2d62c1a44ee8f4cfdc0979121c0462a916c638423c5ebc706c46aa996a7c4f68e030589adc62803f4  mdadm.initd
7d45bf489ef93a4f217ffcf72311eb661b8de7fbf63a5344697252c0d3a684b0123ff60efa5f218da4eb4cda7c88d91c2ef3625d5e44a588e3e1210cb60b0ab9  mdadm.confd
10d2456ca82c353c9b8540ca996de48575a60b83ede5b0bb7e960b4dd28aac2aa73eaeff4bdb99d03ce9630f986c2023b38fe332c5be74e0234985af6273a934  mdadm-raid.initd
df8b5d78000c416652da07e46d94c5c3b8afa48619a360b0a33290420f718bf88069ff4cf62537cc510208fae95b5632a9f340fe97d1aa31661b031bd4dc5d71  lfs64.patch
d3e67b8d4f41996007a4b6dd28666e844925e071cd59530b9611590d536dd206375e372f56bca25cbdf45def41ffe199bd4bf64688436cdd5ac71a6ff699d95d  no-werror.patch
e711c15fada5fc98984f43f90a8ab3b6a2a20e9b91c56b5672fdb0ea127b61934b2f0c6ca986bd91c96c56b66f46326cb616101a62e4bfebe3a2b0d33ed2465c  mdadm-include-sysmacros.patch
d7aeac1ce8e1b17bfdc6178551aaee364b731003e03b1d0c713ee879ac3f82164dff70999ad56c0e91c4bf90c5bca7708bdbb5d2a57e032fd0291828a917b054  fix-monitor-tv_sec.patch
"
